package org.thss.petri.gef.editparts;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.thss.petri.gef.editparts.common.PetriNetNodeEditPart;
import org.thss.petri.gef.editparts.policies.NodeLayoutEditPolicy;
import org.thss.petri.gef.editparts.policies.TokenLayoutEditPolicy;
import org.thss.petri.gef.figures.NodeFigure;
import org.thss.petri.gef.figures.PlaceFigure;
import org.thss.petri.model.PetriPackage;
import org.thss.petri.model.Place;


public class PlaceEditPart extends PetriNetNodeEditPart {
	
	public PlaceEditPart(Place model) {
		super(model);
	}

	@Override
	protected IFigure createFigure() {
		PlaceFigure figure = new PlaceFigure();
		return figure;
	}

	@Override
	public IFigure getContentPane() {
		return ((PlaceFigure) getFigure()).getTokenPane();
	}

	@Override
	protected void refreshVisuals() {
		PlaceFigure figure = (PlaceFigure) getFigure();
		Place model = (Place) getModel();
		int count = model.getTokens().size();
		figure.setTokenCount(count);
		super.refreshVisuals();
	}

	@Override
	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new TokenLayoutEditPolicy());
		super.createEditPolicies();
	}

	@Override
	protected EList getModelChildren() {
		return ((Place) model).getTokens();
	}

	@Override
	protected EList getModelSourceConnections() {
		return ((Place) model).getOutgoings();
	}

	@Override
	protected EList getModelTargetConnections() {
		return ((Place) model).getIncomings();
	}
	
	@Override
	protected ConnectionAnchor getNodeAnchor() {
		NodeFigure figure = (NodeFigure) getFigure();
		return new EllipseAnchor(figure.getNodeShape());
	}

	@Override
	public void notifyChanged(Notification notification) {
		int id = notification.getFeatureID(PetriPackage.class);
		switch (id) {
		case PetriPackage.PLACE__TOKENS:
			refreshChildren();
			refreshVisuals();
			break;
		}
		super.notifyChanged(notification);
	}
	
}
